//
//  HLTuserViewController.m
//  huiliantong
//
//  Created by hlt on 15/8/25.
//  Copyright (c) 2015年 hlt. All rights reserved.
//

#import "HLTuserViewController.h"
#import "HLTusergongnengCollectionViewCell.h"
#import "HLTnewusertitleCollectionViewCell.h"
//#import "HLTusertitleCollectionViewCell.h"
#import "HLTLoginTableViewController.h"
//#import "HLTBandcardTableViewController.h"
#import "HLTBandcardViewController.h"
#import "SliderViewController.h"
//导入支付吗管理需要用到得四个控制器
#import "HLTquanbuTableViewController.h"

#import "HLTorderTableViewController.h"

#import "HLTzhuanzhangTableViewController.h"

#import "HLTUpdatecardpasswordTableViewController.h"

#import "HLTUpdatapasswordTableViewController.h"

#import "HLTCustomerserviceTableViewController.h"

#import "HLTmyuserinfoTableViewController.h"

#import "HLTuserchargeViewController.h"




@interface HLTuserViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,sliderScrollerDelegate>

@property  UICollectionView *testcollection;

@property NSMutableArray *imageary;
@property NSMutableArray *gongnengary;



@end

@implementation HLTuserViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //这个是必须要注册的，不然会崩溃
    //     [self.testcollection registerClass:[HLTusergongnengCollectionViewCell class] forCellWithReuseIdentifier:@"HLTusergongnengCollectionViewCell"];
    
    
    
    self.biaozhi=0;
    
    
    //设置返回的按钮的字体的颜色的
    self.navigationController.navigationBar.tintColor=[UIColor whiteColor];
    //设置导航的背景颜色的
    self.navigationController.navigationBar.barTintColor = navigationBarColor;
    
    //设置导航的标题的字体大小和颜色
    [self.navigationController.navigationBar setTitleTextAttributes:
     
  @{NSFontAttributeName:[UIFont systemFontOfSize:19],
    
    NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    
    
    
    self.zhanneixincount=@"0";
    
    //初始化功能图片数组和文字数组
    _imageary=[NSMutableArray arrayWithObjects:@"卡片绑定",@"我的订单",@"转账",@"支付码",@"卡密码修改",@"登录密码修改",@"个人资料",@"客服",@"充值",nil];
    //    /NSMutableArray *array = [NSMutableArray arrayWithObjects:@"One",@"Two",@"Three",nil];
    _gongnengary=[NSMutableArray arrayWithObjects:@"汇联通卡绑定",@"我的订单",@"转账",@"支付码管理",@"卡密码修改",@"登录密码修改",@"个人资料",@"在线客服",@"充值",nil];
    
    //初始化
    UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing=0;
    self.testcollection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, screen_width,screen_height)collectionViewLayout:flowLayout];
    //注册
    [self.testcollection registerClass:[HLTusergongnengCollectionViewCell class]forCellWithReuseIdentifier:@"HLTusergongnengCollectionViewCell"];
    [self.testcollection registerClass:[HLTnewusertitleCollectionViewCell class]forCellWithReuseIdentifier:@"HLTnewusertitleCollectionViewCell"];
    //设置代理
    self.testcollection.delegate = self;
    self.testcollection.dataSource = self;
    
    self.testcollection.backgroundColor=RGB(255, 255, 255);
    
    [self.view addSubview:self.testcollection];
    
    
    //添加退出按钮
    
    UIButton *searchButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 30)];
    searchButton.titleLabel.font = [UIFont systemFontOfSize: 13.0];
    [searchButton setTitle:@"退出" forState:UIControlStateNormal];
    [searchButton addTarget:self action:@selector(usertuichu) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *searchItem = [[UIBarButtonItem alloc] initWithCustomView:searchButton];
    //     self.navigationItem.rightBarButtonItems = @[searchItem,moreItem];
    self.navigationItem.rightBarButtonItem = searchItem;

    
    
    //设置下拉刷新的内容
    [self setUpTableView];
    
   
}





-(void)setUpTableView{
    //添加下拉的动画图片
    //设置下拉刷新回调
    [self.testcollection addGifHeaderWithRefreshingTarget:self refreshingAction:@selector(refreshData)];
    
    //设置普通状态的动画图片
//    NSMutableArray *idleImages = [NSMutableArray array];
//    for (NSUInteger i = 1; i<=60; ++i) {
//        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"dropdown_anim__000%zd",i]];
//        [idleImages addObject:image];
//    }
//    [self.testcollection.gifHeader setImages:idleImages forState:MJRefreshHeaderStateIdle];
//    
//    //设置即将刷新状态的动画图片
//    NSMutableArray *refreshingImages = [NSMutableArray array];
//    for (NSInteger i = 1; i<=3; i++) {
//        
////        NSString *str=[NSString stringWithFormat:@"dropdown_loading_0%zd",i];
//        
//        
//        UIImage *image1 = [UIImage imageNamed:[NSString stringWithFormat:@"dropdown_loading_0%zd",i]];
//        [refreshingImages addObject:image1];
//    }
//    [self.testcollection.gifHeader setImages:refreshingImages forState:MJRefreshHeaderStatePulling];
//    
//    //设置正在刷新是的动画图片
//    [self.testcollection.gifHeader setImages:refreshingImages forState:MJRefreshHeaderStateRefreshing];
    
    //马上进入刷新状态
    [self.testcollection.gifHeader beginRefreshing];
}



//下拉刷新的回调函数
-(void)refreshData{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^{
        //刷新后需要加载的数据
        [self task];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            //update UI
        });
    });
}






-(void) usertuichu
{
    Account *acc=[[AccountTool sharedAccountTool] currentAccount];
    
    //退出登录
    [[AccountTool sharedAccountTool] tuichu:acc];
    
    [self viewWillDisappear:YES];
    [self viewWillAppear:YES];
    
}



#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 2;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    if (section==0) {
        return 1;
    }else
        return [_imageary count];;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section==0) {
        NSString *identify1 = @"HLTnewusertitleCollectionViewCell";
        HLTnewusertitleCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:identify1 forIndexPath:indexPath];
        
        [cell initview];
        
        cell.parent=self;
        cell.xiaoxicount.text=self.zhanneixincount;
        //        cell.titleimage.image=[UIImage imageNamed:@"bandHui"];
        //        cell.buttontitle.text=@"汇联通卡绑定";
        //设置每一个cell的边框颜色和宽度
        //        cell.layer.borderColor=[UIColor grayColor].CGColor;
        //        cell.layer.borderWidth=1;
        return cell;
    }
    else
    {
        
        static NSString *identify = @"HLTusergongnengCollectionViewCell";
        HLTusergongnengCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:identify forIndexPath:indexPath];
        
        
        NSLog(@"%ld",(long)indexPath.row);
        
        cell.titleimage.image=  [UIImage imageNamed:_imageary[indexPath.row]] ;
        cell.buttontitle.text=_gongnengary[indexPath.row];
        
        //去除背景色
//        cell.backgroundColor=[UIColor clearColor];
        //设置每一个cell的边框颜色和宽度
        cell.layer.borderColor=RGB(241, 241, 241).CGColor;
        cell.layer.borderWidth=0.4;
        return cell;
    }
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    
    if (indexPath.section==1) {
        
        BOOL tiaoguo=YES;
        
        if (indexPath.row!=0&&indexPath.row!=6) {
            Account *acc=[[AccountTool sharedAccountTool] currentAccount];
                
                if (  [self isNull:acc.custIdent]||[acc.custIdent isEqual:@""]||[acc.custIdent length]!=18) {
                    //说明没有填写用户资料
                    
                    //停止定时器
//                    [self.connectionTimer invalidate];
                    
                    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示" message:@"请核对一下个人信息,如果没有设置过身份证号码则必须设置" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"好", nil];
                    [alert show];
                    
//                    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
//                    HLTmyuserinfoTableViewController *vc = [storyBoard instantiateViewControllerWithIdentifier:@"HLTmyuserinfoTableViewController"];
//                    
//                                    vc.hidesBottomBarWhenPushed = YES;
//                    
//                    //            HLTCustomerserviceTableViewController *vc =[[HLTCustomerserviceTableViewController alloc]init];
//                    [self.navigationController pushViewController:vc animated:NO];
                    tiaoguo=NO;
                }
                else
                {
                    
                    if ([acc.cardNo isEqual:@""]) {
                        
                        //停止定时器
//                        [self.connectionTimer invalidate];
                        
                        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示" message:@"使用个人中心的功能需要先绑定卡" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"好", nil];
                        [alert show];
                        
                        
//                        //直接进入绑定卡界面
//                        UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
//                        HLTBandcardViewController *vc = [storyBoard instantiateViewControllerWithIdentifier:@"HLTBandcardViewController"];
//                                            vc.hidesBottomBarWhenPushed = YES;
//                        [self.navigationController pushViewController:vc animated:NO];
                        
                         tiaoguo=NO;
                    }
                    
                    
                }
                
            }
            
            
            
        
        
        if (tiaoguo) {
            
        
        if (indexPath.row==0) {
            
            
            
            
            [EYInputPopupView popViewWithTitlenumber:@"请输入登陆密码" contentText:@"登陆密码"
                                                type:EYInputPopupView_Type_single_line_text
                                         cancelBlock:^{
                                             
                                         } confirmBlock:^(UIView *view, NSString *text) {
                                             
                                             Account *acc=[[AccountTool sharedAccountTool] currentAccount];
                                             
                                             
                                             if ([acc.pwd isEqual:text]) {
                                                 
                                                 
                                                 
                                                 UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                                                 HLTBandcardViewController *vc = [storyBoard instantiateViewControllerWithIdentifier:@"HLTBandcardViewController"];
                                                 vc.hidesBottomBarWhenPushed = YES;
                                                 [self.navigationController pushViewController:vc animated:NO];
                                                 
                                                 
                                             }else
                                             {
                                                 
                                                 
                                                 
                                                 self.biaozhi=self.biaozhi+1;
                                                 if (self.biaozhi>=5) {
                                                     
                                                     UIAlertView *alert= [[UIAlertView alloc] initWithTitle:@"提示" message:@"您输入的密码错误次数超过5次，将清除登陆信息并重新登陆" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"好", nil];
                                                     
                                                     [alert show];
                                                     Account *acc=[[AccountTool sharedAccountTool] currentAccount];
                                                     //退出登录
                                                     [[AccountTool sharedAccountTool] tuichu:acc];
                                                     
                                                     [self viewWillDisappear:YES];
                                                     [self viewWillAppear:YES];
                                                     
//                                                     [self.navigationController popViewControllerAnimated:YES];
                                                     
                                                 }
                                                 else
                                                 {
                                                     
                                                     NSString *str=[NSString stringWithFormat:@"您输入的密码不对,还有%ld次输入机会",5-self.biaozhi];
                                                     
                                                     UIAlertView *alert= [[UIAlertView alloc] initWithTitle:@"提示" message:str delegate:nil cancelButtonTitle:nil otherButtonTitles:@"好", nil];
                                                     
                                                     [alert show];
                                                 }
                                                 
                                                 }
                                             
                                             
                                         } dismissBlock:^{
                                             
                                         }];
            
        }else if (indexPath.row==3)
        {
            NSMutableArray * array=[NSMutableArray array];
            
            NSArray * titiles=@[@"全部",@"有效",@"交易完成"];
            for (int i=0; i<titiles.count; i++) {
                
                if (i==0) {
                    HLTquanbuTableViewController *nex1= [[HLTquanbuTableViewController alloc] init];
                    nex1.type=quanbu;
                    [array addObject:nex1];
                }
                else if (i==1)
                {
                    HLTquanbuTableViewController *nex1= [[HLTquanbuTableViewController alloc] init];
                    nex1.type=youxiao;
                    
                    
                    [array addObject:nex1];
                    
                }else if (i==2)
                {
                    HLTquanbuTableViewController *nex1= [[HLTquanbuTableViewController alloc] init];
                    nex1.type=wuxiaio;
                    [array addObject:nex1];
                }
                //                    else if (i==3)
                //                {
                //                     HLTquanbuTableViewController *nex1= [[HLTquanbuTableViewController alloc] init];
                //                    nex1.type=weishiyong;
                //                    [array addObject:nex1];
                //                }
            }
            
            
            SliderViewController * next=[[SliderViewController alloc] initWithViewControllers:array];
            
            for (int i=0; i<[array count]; i++) {
                
                HLTquanbuTableViewController *vcc=array[i];
                vcc.parent=next;
                
            }
            
            
            next.titileArray=titiles;
            next.lineHeight=2;
            next.selectTextColor=navigationBarColor;
            
            next.selectBgColor=[UIColor whiteColor];
            next.defaultUnSelectBgColor=[UIColor lightGrayColor];
            // next.separateColor=[UIColor redColor];
            next.selectIndex=0;
            next.sliderDelegate=self;
            next.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:next animated:YES];
            
        }
        else if (indexPath.row==2)
        {
            
            UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            HLTzhuanzhangTableViewController *vc = [storyBoard instantiateViewControllerWithIdentifier:@"HLTzhuanzhangTableViewController"];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:NO];
            
        }else if (indexPath.row==1)
        {
            NSMutableArray * array=[NSMutableArray array];
            
//            NSArray * titiles1=@[@"全部",@"退款中",@"退款完成",@"拒绝退款",@"交易完成",@"待确认"];

            NSArray * titiles=@[@"待确认",@"退款中",@"退款成功",@"拒绝退款",@"交易完成"];
            for (int i=0; i<titiles.count; i++) {
                
                if (i==0) {
                    HLTorderTableViewController *nex1= [[HLTorderTableViewController alloc] init];
                    nex1.type=ketuikuan;
                    [array addObject:nex1];
                }
                else if (i==1)
                {
                    HLTorderTableViewController *nex1= [[HLTorderTableViewController alloc] init];
                    nex1.type=tuikuanzhong;
                    
                    
                    [array addObject:nex1];
                    
                }else if (i==2)
                {
                    HLTorderTableViewController *nex1= [[HLTorderTableViewController alloc] init];
                    nex1.type=tuikuanchenggong;
                    [array addObject:nex1];
                }else if (i==3)
                {
                    HLTorderTableViewController *nex1= [[HLTorderTableViewController alloc] init];
                    nex1.type=jujuetuikiuan;
                    [array addObject:nex1];
                }else if (i==4)
                {
                    HLTorderTableViewController *nex1= [[HLTorderTableViewController alloc] init];
                    nex1.type=jiaoyiwancheng;
                    [array addObject:nex1];
                }
            }
            
            
            SliderViewController * next=[[SliderViewController alloc] initWithViewControllers:array];
            
            
            for (int i=0; i<[array count]; i++) {
                
                    HLTorderTableViewController *vcc=array[i];
                    vcc.parent=next;
                
            }

            
            
            
            //            next.tag=1005;
            next.titileArray=titiles;
            next.lineHeight=2;
            next.selectTextColor=navigationBarColor;
            
            next.selectBgColor=[UIColor whiteColor];
            next.defaultUnSelectBgColor=[UIColor lightGrayColor];
            // next.separateColor=[UIColor redColor];
            next.selectIndex=0;
            next.sliderDelegate=self;
            next.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:next animated:YES];
            
            
            
        }else if (indexPath.row==4)
        {
            
            Account *acc=[[AccountTool sharedAccountTool] currentAccount];
            
            if ([acc.cardNo  isEqual:@""]) {
                UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示" message:@"您个还没有绑定卡，请先绑定卡之后再进行此操作" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"好", nil];
                [alert show];
                
            }
            else
            {
            UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            HLTUpdatecardpasswordTableViewController *vc = [storyBoard instantiateViewControllerWithIdentifier:@"HLTUpdatecardpasswordTableViewController"];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:NO];
            }
            
        }else if (indexPath.row==5)
        {
            
            UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            HLTUpdatapasswordTableViewController *vc = [storyBoard instantiateViewControllerWithIdentifier:@"HLTUpdatapasswordTableViewController"];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:NO];
            
        }else if (indexPath.row==6)
        {
            UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            HLTmyuserinfoTableViewController *vc = [storyBoard instantiateViewControllerWithIdentifier:@"HLTmyuserinfoTableViewController"];
            
            vc.hidesBottomBarWhenPushed = YES;
            
            //            HLTCustomerserviceTableViewController *vc =[[HLTCustomerserviceTableViewController alloc]init];
            [self.navigationController pushViewController:vc animated:NO];
            
        }else if (indexPath.row==7)
        {
            UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            HLTCustomerserviceTableViewController *vc = [storyBoard instantiateViewControllerWithIdentifier:@"HLTCustomerserviceTableViewController"];
            
            vc.hidesBottomBarWhenPushed = YES;
            
            //            HLTCustomerserviceTableViewController *vc =[[HLTCustomerserviceTableViewController alloc]init];
            [self.navigationController pushViewController:vc animated:NO];
            
        }else if (indexPath.row==8)
        {
            
            Account *acc=[[AccountTool sharedAccountTool] currentAccount];
                                           //
                                           NSString *url=[NSString stringWithFormat:@"%@&user_name=%@",usercharge,acc.custPhone];
                                           
                                           UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                                           HLTuserchargeViewController *vc = [storyBoard instantiateViewControllerWithIdentifier:@"HLTuserchargeViewController"];
                                           vc.hidesBottomBarWhenPushed = YES;
                                           [vc.navigationItem setTitle:@"充值"];
                                           vc.url=url;
                                           //            [vc setmywebview];
                                           
                                           //            HLTCustomerserviceTableViewController *vc =[[HLTCustomerserviceTableViewController alloc]init];
                                           [self.navigationController pushViewController:vc animated:NO];
            
        }
        
    }
    
    }
    
    
    
}



#pragma mark <UICollectionViewDelegate>

//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    if (indexPath.section==0) {
        
        CGSize headerSize = CGSizeMake(screen_width,screen_width/2.0);
        return headerSize;
        
        
    }
    else
    {
        CGSize headerSize = CGSizeMake(screen_width/3.0,screen_width/3.0-10);
        return headerSize;
    }
    
}


#pragma mark uialert的委托方法
//uialert的委托方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
    if (buttonIndex==1) {
        //点击了好，进入下一个页面
        
        
        
        
        
        
        
        
        
    }
}





#pragma mark slider的委托

-(void)sliderScrollerDidIndex:(NSInteger)index andSlider:(SliderViewController *)slider{
    
    NSString * tiitle=slider.titileArray[index];
    slider.title=tiitle;
}



//定时器的回调方法
-(void) task
{
    //定时刷新用户数据
    
    Account *acc=[[AccountTool sharedAccountTool]currentAccount];
    
    //自定检查登录的函数
    [self customlogin:acc.custPhone password:acc.pwd];
    
    if ([acc.custPhone isEqual:@""]) {
       //没有登陆信息
        
        
        
        
    }else
    {
    
    NSString *url=[jianchazhanneixin stringByAppendingFormat:@"&username=%@",acc.custPhone];
    //将url转换一下格式，否则请求的格式就不兼容
    //    url = [url stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
    [HLTNetWorkingtool getdic:url success:^(NSDictionary *ary) {
        //        NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
        //        NSString *str = [numberFormatter stringFromNumber:ary[@"11"]];
        //
//        [self.testcollection.header endRefreshing];
        if ([ary count]>0) {
            
            
            
            NSArray *keyary=[ary allKeys];
            int num=0;
            //通过下面这个循环得到消息总数
            for (int i=0; i<[keyary count]; i++) {
                num+=[ary[keyary[i]] intValue];
            }
            
            NSString *strcoun=[NSString stringWithFormat:@"%d",num];
            
            self.zhanneixincount=strcoun;
            NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:0];
            [self.testcollection reloadSections:indexSet];
            
        }
        
    } faliure:^(NSString *error) {
//        [self.testcollection.header endRefreshing];
    }];
    
    
    
    }
    
    
    
    
}


-(BOOL)isNull:(id)object
{
    // 判断是否为空串
    if ([object isEqual:[NSNull null]]) {
        return YES;
    }
    else if ([object isKindOfClass:[NSNull class]])
    {
        return YES;
    }
    else if (object==nil){
        return YES;
    }
    return NO;
}







//即将出现调用的方法
-(void) viewWillAppear:(BOOL)animated
{
   //开启定时器
    NSDate *scheduledTime = [[NSDate alloc]init];
    NSString *customUserObject = @"To demo userInfo";
    self.connectionTimer = [[NSTimer alloc] initWithFireDate:scheduledTime
                                                    interval:30
                                                      target:self
                                                    selector:@selector(task)
                                                    userInfo:customUserObject
                                                     repeats:YES];
    NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
    [runLoop addTimer:self.connectionTimer forMode:NSDefaultRunLoopMode];
    
    
    
//    [self.connectionTimer setFireDate:[NSDate distantPast]];
    
    
    
    //隐藏tabbar的方法
    //    self.hidesBottomBarWhenPushed = NO;
    
    //     [self.tab setTabBarHidden:YES];
    Account *acc=[[AccountTool sharedAccountTool] currentAccount];
    
    if ([acc.pwd isEqualToString:@""]) {
         
        //停止定时器
        [self.connectionTimer invalidate];
        //关闭定时器
//        [self.connectionTimer setFireDate:[NSDate distantFuture]];
        
//        UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
//        HLTLoginTableViewController *vc = [storyBoard instantiateViewControllerWithIdentifier:@"HLTLoginTableViewController"];
//        
//        [self.navigationController pushViewController:vc animated:NO];
         self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:0];
    }else
    {
        if (!acc) {
            
            //停止定时器
            [self.connectionTimer invalidate];
            //关闭定时器
//            [self.connectionTimer setFireDate:[NSDate distantFuture]];
//            UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
//            HLTLoginTableViewController *vc = [storyBoard instantiateViewControllerWithIdentifier:@"HLTLoginTableViewController"];
//            vc.yinchang=YES;
            
            
//            self.tabBarController.selectedViewController = UIViewController-YouWant
            self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:0];
            
            
            
//            [self.navigationController pushViewController:vc animated:NO];
            
        }else
        {
            
            [self.testcollection reloadData];
            
            
            
        }
        
        
        
        
        
        
    }
    
    
    
    
    
}








//即将消失调用的方法
- (void)viewWillDisappear:(BOOL)animated
{
    //停止定时器
    [self.connectionTimer invalidate];
    //关闭定时器
//    [self.connectionTimer setFireDate:[NSDate distantFuture]];
    
    //隐藏tabbar(这个得值在使用storyborad时yes是显示，没有使用时yes是不显示)
    //    self.hidesBottomBarWhenPushed = YES;
    
}











-(void) customlogin:(NSString *)username password:(NSString *)pwd
{
    
    
    //base64 编码
    NSString *base64pwd= [SetBase64andMd5 base64:pwd ncryptOrDecrypt:kCCEncrypt];
    NSString *url=[loginurl stringByAppendingFormat:@"&username=%@&password=%@",username,base64pwd];
    [HLTNetWorkingtool getary:url success:^(NSArray *ary) {
        
        //        NSArray *aaary=ary;
        NSDictionary *dic=ary[0];
        NSLog(@"%@",dic[@"bind_card"]);
        if ([dic[@"status"] isEqual:@"0"]) {
            Account *acc=[[AccountTool sharedAccountTool] currentAccount];
            
            
            if ([dic[@"bind_card"] isEqual:@"0"]||[dic[@"bind_card"] isEqual:@"00"]) {
                
                //加这个条件是为了防止已经点击了退出按钮之后这里还执行导致失败
                if (![acc.pwd isEqual:@""]) {
                acc.pwd=pwd;
                acc.custPhone=username;
                acc.custFlag=dic[@"user_rank"];
                acc.cardNo=@"";
                [[AccountTool sharedAccountTool] addAccounts:acc];
                }
                 [self.testcollection.header endRefreshing];
                NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:0];
                [self.testcollection reloadSections:indexSet];
                
            }
            else
            {
                
                
                
                NSString *url=[chaxunyue stringByAppendingFormat:@"&user_name=%@&cardNum=%@",username,dic[@"bind_card"]];
                [HLTNetWorkingtool getdic:url success:^(NSDictionary *ary) {
//                     [self.testcollection.header endRefreshing];
                    NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
                    NSString *str = [numberFormatter stringFromNumber:ary[@"code"]];
                    
                    if ([str isEqual:@"2"]) {
                        Account *acc=[[AccountTool sharedAccountTool] currentAccount];
                        //加这个条件是为了防止已经点击了退出按钮之后这里还执行导致失败
                        if (![acc.pwd isEqual:@""]) {
                        acc.pwd=pwd;
                        acc.cardNo=dic[@"bind_card"];
                        acc.custFlag=dic[@"user_rank"];
                        acc.custPhone=username;
                        acc.YKTCardBalance=ary[@"deposit"];
                        [[AccountTool sharedAccountTool] addAccounts:acc];
                        }
                        NSLog(@"%@",ary);
//                        [self.navigationController popViewControllerAnimated:YES];
                        [self.testcollection.header endRefreshing];
                        NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:0];
                        [self.testcollection reloadSections:indexSet];
                            
                    }else
                    {
                        [self.testcollection.header endRefreshing];
                        
                    }
                } faliure:^(NSString *error) {
                    [self.testcollection.header endRefreshing];
                    
                }];
            }
            
        
        
        }else
        {
            //清空登陆信息
            Account *acc=[[AccountTool sharedAccountTool] currentAccount];
            
            //退出登录
            [[AccountTool sharedAccountTool] tuichu:acc];
            
            
            
            UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"您的登录信息已改变，请重新登录"   message:dic[@"msg"]  delegate:nil cancelButtonTitle:@"好" otherButtonTitles: nil];
            [alert show];
            
            //停止定时器
            [self.connectionTimer invalidate];
            //关闭定时器
//            [self.connectionTimer setFireDate:[NSDate distantFuture]];
            
            
//            Account *acc=[[AccountTool sharedAccountTool] currentAccount];
//            
//            //退出登录
//            [[AccountTool sharedAccountTool] tuichu:acc];
//            
//            UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
//            HLTLoginTableViewController *vc = [storyBoard instantiateViewControllerWithIdentifier:@"HLTLoginTableViewController"];
//            
//            [self.navigationController pushViewController:vc animated:NO];
            
            
            self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:0];
            
             [self.testcollection.header endRefreshing];
            
        }
        
        
        
        
    } faliure:^(NSString *error) {
        
        //网络请求失败时提示，但是不退出登陆
        
        //清空登陆信息
//        Account *acc=[[AccountTool sharedAccountTool] currentAccount];
        
        //退出登录
//        [[AccountTool sharedAccountTool] tuichu:acc];
        
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"您的网络不太好哟-_-"   message:nil  delegate:nil cancelButtonTitle:@"好" otherButtonTitles: nil];
        [alert show];
        
        
        //停止定时器
//        [self.connectionTimer invalidate];
        
        //关闭定时器
//        [self.connectionTimer setFireDate:[NSDate distantFuture]];
        
        
//        Account *acc=[[AccountTool sharedAccountTool] currentAccount];
//        
//        //退出登录
//        [[AccountTool sharedAccountTool] tuichu:acc];
        
        
        
//        UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
//        HLTLoginTableViewController *vc = [storyBoard instantiateViewControllerWithIdentifier:@"HLTLoginTableViewController"];
//        
//        [self.navigationController pushViewController:vc animated:NO];

        //强制将页面转到第一页
//        self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:0];
        
         [self.testcollection.header endRefreshing];
    }];
    
}

















- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
